<template>
  <view class="my-search-container" :style="{ 'background-color': bgcolor }">
    <image
      @click.stop="requireLocate"
      class="location"
      src="/static/my-icons/location.png"
    />
    <view
      class="my-search-box"
      :style="{ 'border-radius': radius + 'px' }"
      @click="searchBoxHandler"
    >
      <uni-icons type="search" size="17"></uni-icons>
      <text class="placeholder">搜索</text>
    </view>
  </view>
</template>

<script>
export default {
  name: "my-search",
  props: {
    // 背景颜色
    bgcolor: {
      type: String,
      default: "#ffd365",
    },
    // 圆角尺寸
    radius: {
      type: Number,
      // 单位是 px
      default: 18,
    },
  },
  data() {
    return {
      userId: uni.getStorageSync("userId"),
      location: "",
      latitude: 0,
      longitude: 0,
    };
  },
  methods: {
    requireLocate() {
      uni.chooseLocation({
        success: (res) => {
          this.location = res.address;
          this.latitude = res.latitude;
          this.longitude = res.longitude;
          uni.setStorageSync("location", this.location);
          uni.setStorageSync("latitude", this.latitude);
          uni.setStorageSync("longitude", this.longitude);
          console.log("位置：", this.location, this.latitude, this.longitude);
          uni.$http;
          put("/farmer/updateLocation", {
            userId: this.userId,
            location: this.location,
            latitude: this.latitude,
            longitude: this.longitude,
          })
            .then((res) => {
              console.log(res);
              if (res.data.code == 200) {
                console.log("位置更新成功", res);
                uni.showToast({
                  title: "位置更新成功",
                  icon: "success", //将值设置为 success 或者 ''
                  duration: 2000, //持续时间为 2秒
                });
              } else {
                console.log("位置更新失败", res);
                uni.showToast({
                  title: "位置更新失败",
                  icon: "none", //将值设置为 success 或者 ''
                  duration: 2000, //持续时间为 2秒
                });
              }
            })
            .catch((err) => {
              console.error("位置更新失败", err);
              uni.showToast({
                  title: "位置更新失败",
                  icon: "none", //将值设置为 success 或者 ''
                  duration: 2000, //持续时间为 2秒
                });
            });
        },
      });
    },
    // 点击了模拟的 input 输入框
    searchBoxHandler() {
      // 触发外界通过 @click 绑定的 click 事件处理函数
      this.$emit("click");
    },
  },
};
</script>

<style lang="scss">
.my-search-container {
  //background-color: #c00000;
  height: 43px;
  padding: 0 10px;
  display: flex;
  align-items: center;
}
.location {
  width: 25px;
  height: 25px;
  margin-right: 10px;
}

.my-search-box {
  height: 30px;
  background-color: #ffffff;
  //border-radius: 15px;
  width: 90%;
  display: flex;
  align-items: center;
  justify-content: center;

  .placeholder {
    font-size: 14px;
    color: #b7b7b7;
    margin-left: 5px;
  }
}
</style>